Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Authenication with JWT returning empty array with a GET request [NodeJS]

Trying to practice JWT with NodeJS but when I try to run a GET request using the headers of the Authorization: Bearer , it returns an array instead of the object with the token, I tried with the Postman app but to no avail

This is the code, the get and post request code:

    const express = require('express');
require('dotenv').config();
const jwt = require('jsonwebtoken');
const crypto = require('crypto');
const app = express();

const user = [
    {
        username: 'John',
        email: 'john@gmail.com'
    }, 
    {
        username: "Joshua",
        email: 'joshua@gmail.com'
    }
]

app.use(express.json())

app.get('/api', authenicateToken, (req, res,) => {
    res.json(user.filter(post => post.username === req.body.name))
})

app.post('/login', (req, res) => {
    const username = req.body.username
    const use = { name: username }

    const accessToken = jwt.sign(use, process.env.ACCESS_KEY)
    res.json({ accessToken: accessToken })
})

function authenicateToken (req, res, next) {
    const authHeader = req.headers['authorization']
    const token = authHeader && authHeader.split(' ')[1]
    if(token === null) return res.sendStatus(403)

    jwt.verify(token, process.env.ACCESS_KEY, (err, user) => {
        if(err) return res.sendStatus(403)
        req.user = user
        next()
    })
}

app.listen(4000, () => {
    console.log('Can you hear me?');
})

enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

filter returns an array, use find method it returns single item.

res.json(user.find(post => post.username === req.body.name))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!